package com.zhongxi.guoyuan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhongxi.guoyuan.R;
import com.zhongxi.guoyuan.entity.shopcart.RewardPayResult;
import com.zhongxi.guoyuan.url.Url;
import com.zhongxi.guoyuan.utils.GlideRoundTransform;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.Iterator;
import java.util.List;

/**
 * Created by Administrator on 2017/3/8.
 */

public class RewardCoinPayAdapter extends BaseAdapter {

    private Context context;
    private List<RewardPayResult.RewardPayData> list;
    private RewardPayResult.Summary summary;
    private JSONObject str;

    public RewardCoinPayAdapter(Context context, List<RewardPayResult.RewardPayData> data) {
        this.context = context;
        this.list = data;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        try{
            if(convertView == null){
                holder = new ViewHolder();
                convertView = LayoutInflater.from(context).inflate(R.layout.item_reward_pay, parent, false);
                holder.ivImg = (ImageView) convertView.findViewById(R.id.ivImg);
                holder.tvName = (TextView) convertView.findViewById(R.id.tvName);
                holder.tvNum = (TextView) convertView.findViewById(R.id.tvNum);
                holder.tvAttr = (TextView) convertView.findViewById(R.id.tvAttr);
                holder.tvCash = (TextView) convertView.findViewById(R.id.tvCash);
                holder.tvReward = (TextView) convertView.findViewById(R.id.tvReward);
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }
            if(null!=list.get(position)&&!"".equals(list.get(position))){
                RewardPayResult.RewardPayData data = list.get(position);
                if(null!=data.getThumbnail()&&!"".equals(data.getThumbnail())){
                    Glide.with(context).load(Url.IMG_URL+data.getThumbnail()).placeholder(R.mipmap.iv_eg).transform(new GlideRoundTransform(context, 20)).into(holder.ivImg);
                }
                if(null!=data.getGoodsname()&&!"".equals(data.getGoodsname())){
                    holder.tvName.setText(data.getGoodsname());
                }else{
                    holder.tvName.setText("**");
                }
                if(null!=data.getShouldprice()&&!"".equals(data.getShouldprice())){
                    holder.tvCash.setText(data.getShouldprice());
                }else{
                    holder.tvCash.setText("");
                }
                if(null!=data.getGoodsnum()&&!"".equals(data.getGoodsnum())){
                    holder.tvNum.setText("X"+data.getGoodsnum());
                }else{
                    holder.tvNum.setText("");
                }
            }
            if(null!=str&&!"".equals(str)){
                JSONArray info = str.getJSONArray("goodsInfo");
                if(null!=info.get(position)&&!"".equals(info.get(position))){
                    JSONObject data = info.getJSONObject(position);
                    JSONObject attr = data.getJSONObject("gattr");
                    Iterator<String> iterator = attr.keys();
                    String str = "";
                    while (iterator.hasNext()){
                        String key = iterator.next();
                        str += "/"+attr.getString(key);
                    }
                    holder.tvAttr.setText(str);
                }
            }else{
                holder.tvAttr.setText("");
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return convertView;
    }

    public void setSummary(RewardPayResult.Summary summaryResult) {
        this.summary = summaryResult;
    }

    public void setStr(JSONObject str) {
        this.str = str;
    }

    class ViewHolder{
        private ImageView ivImg;
        private TextView tvName;
        private TextView tvNum;
        private TextView tvAttr;
        private TextView tvCash;
        private TextView tvReward;
//        private TextView tvPost;
    }
}
